package com.socialin.android.hackaton.draw.drawing;

import android.graphics.Path;
import android.view.MotionEvent;

public class DrawingController {
	private boolean mIsFirstPoint = false;
	
	private float mLastStartX, mLastStartY;
	
	private float mPathLastX, mPathLastY;
	
//	public Path mCurrentDrawingPath = new Path();

	int color;
	
	public DrawingController() {
		color = 0xff000000 | (int) (Math.random() * 0xffffff);
	}
	
	public void handleTouch(int action, int x, int y, Path p) {
		p.rewind();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			p.rewind();
			p.moveTo(x, y);
			
			mPathLastX = x;
			mPathLastY = y;
			
			mLastStartX = x;
			mLastStartY = y;
			mIsFirstPoint = true;
			break;
			
		case MotionEvent.ACTION_MOVE:
			float middlePointX = (x + mLastStartX) / 2;
			float middlePointY = (y + mLastStartY) / 2;

			if (mIsFirstPoint) {
				p.setLastPoint(mPathLastX, mPathLastY);
				p.lineTo(middlePointX, middlePointY);
			} else {
				p.setLastPoint(mPathLastX, mPathLastY);
				p.quadTo(mLastStartX, mLastStartY, middlePointX, middlePointY);
			}
			
			mPathLastX = middlePointX;
			mPathLastY = middlePointY;
			
			mLastStartX = x;
			mLastStartY = y;
			
			mIsFirstPoint = false;
			break;
			
		case MotionEvent.ACTION_OUTSIDE:
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			break;
		}
	}


	public int getColor() {
		return color;
	}
}
